package common.kingdee.repository;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.http.HttpException;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;

import common.kingdee.domain.KingdeeUser;
import common.security.repository.UserLoader;
import common.webtools.MyHttpClient;
import net.sf.json.JSONObject;

@Repository
public class KingdeeUserLoader implements UserLoader<KingdeeUser> {
	private KingdeeEncrypt kindeeYunEncrypt = new KingdeeEncrypt();
	private MyHttpClient myHttpClient = new MyHttpClient();
	@Value("${EID}")
	private String EID;
	@Value("${keyFileDir}")
	private String keyFileDir;
	@Value("${host}")
	private String host;
	@Value("${getAllUserPath}")
	private String getAllUserPath;

	@Override
	public Set<KingdeeUser> load() {
		String url = host + getAllUserPath;
		Set<KingdeeUser> users = new HashSet<>();
		int page = 1;
		boolean needNextPage = true;
		while (needNextPage) {
			List<NameValuePair> nvps = new ArrayList<NameValuePair>();
			nvps.add(new BasicNameValuePair("nonce", String.valueOf(new Date().getTime())));
			nvps.add(new BasicNameValuePair("eid", EID));
			String params = personGetAllParam(page);
			nvps.add(new BasicNameValuePair("data", kindeeYunEncrypt.encrypt(params, keyFileDir + EID + ".key")));
			System.out.println(params);
			if (!keyFileDir.endsWith("/")) {
				keyFileDir += "/";
			}else{
				page++;
			}
			try {
				String reponse = myHttpClient.post(url, nvps);
				JSONObject jsonObj = JSONObject.fromObject(reponse);
				if ("false".equals(jsonObj.get("success").toString())) {
					throw new IOException(jsonObj.get("error").toString());
				}
				List<JSONObject> list = (List<JSONObject>) jsonObj.get("data");
				if (list.size() < 1000) {
					needNextPage = false;
				}
				for (JSONObject jSONObject : list) {
					int status =(Integer)jSONObject.get("status");
					if (status!=1)
						continue;
					String mobile = (String) jSONObject.get("phone");
					String chineseName = (String) jSONObject.get("name");
					String departmentLongName = ((String) jSONObject.get("department")).replaceAll("\\\\", "/");
					String openId = (String) jSONObject.get("openId");
					String jobTitle = (String) jSONObject.get("jobTitle");
					String photoUrl = (String) jSONObject.get("photoUrl");
					KingdeeUser user = new KingdeeUser(openId, mobile, chineseName, "", departmentLongName, photoUrl);
					user.setOpenId(openId);
					user.setJobTitle(jobTitle);
					users.add(user);
				}
			} catch (IOException | HttpException e) {
				e.printStackTrace();
			}
		}
		return users;
	}

	private String personGetAllParam(int page) {
		JSONObject jo = new JSONObject();
		jo.put("eid", this.EID);
		jo.put("begin", (page - 1) * 1000);
		jo.put("count", 1000);
		return jo.toString();
	}
}
